package com.wangtao.firstapp.activity.sife_secondactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.WebViewData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * WebView 页面
 * Created by lanouhn on 16/9/6.
 */
public class WebViewActivity extends Activity {

    private WebView mWebView;
    private WebViewData webViewData;

    private ImageButton back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_webview);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        back= (ImageButton) findViewById(R.id.back_act_webView);
        mWebView = (WebView) findViewById(R.id.webview);
    }

    private void initData() {
        Intent intent = getIntent();
        String item_ID = intent.getStringExtra("item_ID");

        //WebView
        String urlWebView = "http://open4.bantangapp.com/topic/newInfo";
        String paramsWebView = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id= " + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
        try {
            HttpUtils.doPostAsyn(urlWebView, paramsWebView, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_WebView = new Gson();
                    webViewData = gson_WebView.fromJson(result, WebViewData.class);
                    if (webViewData.getData() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    //返回上一个页面
    private void initEvent() {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
                    mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    mWebView.getSettings().setLoadWithOverviewMode(true);
                    mWebView.loadData(webViewData.getData().getArticle_content(), "text/html; charset=UTF-8", null);

                    break;
            }
        }
    };
}
